﻿//==============================================================================
// File: ActionService.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Input;
using WpfRcp.Core.Services;

namespace WpfRcp.Services
{
    [ComVisible(true)]
    public class ActionService : IActionService
    {
        private readonly IDictionary<string, ActionHolder> m_Actions;

        public ActionService()
        {
            m_Actions = new Dictionary<string, ActionHolder>();
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParameter">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParameter">Parameter for the AlternativeAction delegate.</param>
        /// <param name="args">An array of parameters for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object InvokeAction(string action, object canExecuteParameter,
                                   object alternativeActionParameter, params object[] args)
        {
            if (m_Actions.ContainsKey(action))
            {
                var holder = m_Actions[action];
                var canExecute = true;

                if (holder.CanExecute != null)
                    canExecute = holder.CanExecute(canExecuteParameter);

                if (canExecute)
                {
                    holder.Action.DynamicInvoke(args);
                }
                else if (holder.AlternativeAction != null)
                {
                    holder.AlternativeAction(alternativeActionParameter);
                }
            }

            return null;
        }

        public void RegisterAction(string key, ICommand command)
        {
            Action<object> execute = p => command.Execute(p);
            Func<object, bool> canExecute = p => command.CanExecute(p);

            RegisterAction(key, execute, canExecute);
        }

        public void RegisterAction(string key, ICommand command, Action<object> alternativeAction)
        {
            Action<object> execute = p => command.Execute(p);
            Func<object, bool> canExecute = p => command.CanExecute(p);

            RegisterAction(key, execute, canExecute, alternativeAction);
        }

        public void RegisterAction(string key, Delegate action)
        {
            RegisterAction(key, action, null, null);
        }

        public void RegisterAction(string key, Delegate action, Func<object, bool> canExecute)
        {
            RegisterAction(key, action, canExecute, null);
        }

        public void RegisterAction(string key, Delegate action,
                                   Func<object, bool> canExecute, Action<object> alternativeAction)
        {
            var holder = new ActionHolder
                             {
                                 Action = action,
                                 CanExecute = canExecute,
                                 AlternativeAction = alternativeAction
                             };

            m_Actions.Add(key, holder);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object ExplicitInvokeAction(string action)
        {
            return InvokeAction(action, null, null);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object ExplicitInvokeAction(string action, object arg1)
        {
            return InvokeAction(action, null, null, arg1);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <param name="arg2">Second parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object ExplicitInvokeAction(string action, object arg1, object arg2)
        {
            return InvokeAction(action, null, null, arg1, arg2);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <param name="arg2">Second parameter for the Action delegate.</param>
        /// <param name="arg3">Third parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object ExplicitInvokeAction(string action, object arg1, object arg2, object arg3)
        {
            return InvokeAction(action, null, null, arg1, arg2, arg3);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParam">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParam">Parameter for the AlternativeAction delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object FullExplicitInvokeAction(string action, object canExecuteParam,
                                               object alternativeActionParam)
        {
            return InvokeAction(action, canExecuteParam, alternativeActionParam);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParam">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParam">Parameter for the AlternativeAction delegate.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object FullExplicitInvokeAction(string action, object canExecuteParam,
                                               object alternativeActionParam, object arg1)
        {
            return InvokeAction(action, canExecuteParam, alternativeActionParam, arg1);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParam">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParam">Parameter for the AlternativeAction delegate.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <param name="arg2">Second parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object FullExplicitInvokeAction(string action, object canExecuteParam,
                                               object alternativeActionParam, object arg1, object arg2)
        {
            return InvokeAction(action, canExecuteParam, alternativeActionParam, arg1, arg2);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParam">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParam">Parameter for the AlternativeAction delegate.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <param name="arg2">Second parameter for the Action delegate.</param>
        /// <param name="arg3">Third parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object FullExplicitInvokeAction(string action, object canExecuteParam,
                                               object alternativeActionParam, object arg1, object arg2, object arg3)
        {
            return InvokeAction(action, canExecuteParam, alternativeActionParam, arg1, arg2, arg3);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter. This method is intended
        /// to be called only from JavaScript.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParam">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParam">Parameter for the AlternativeAction delegate.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object JavaScriptInvokeAction1(string action, object canExecuteParam,
                                              object alternativeActionParam, object arg1)
        {
            return InvokeAction(action, canExecuteParam, alternativeActionParam, arg1);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter. This method is intended
        /// to be called only from JavaScript.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParam">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParam">Parameter for the AlternativeAction delegate.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <param name="arg2">Second parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object JavaScriptInvokeAction2(string action, object canExecuteParam,
                                              object alternativeActionParam, object arg1, object arg2)
        {
            return InvokeAction(action, canExecuteParam, alternativeActionParam, arg1);
        }

        /// <summary>
        /// Executes an action identified by <paramref name="action"/> parameter. This method is intended
        /// to be called only from JavaScript.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        /// <param name="canExecuteParam">Parameter for the CanExecute delegate.</param>
        /// <param name="alternativeActionParam">Parameter for the AlternativeAction delegate.</param>
        /// <param name="arg1">First parameter for the Action delegate.</param>
        /// <param name="arg2">Second parameter for the Action delegate.</param>
        /// <param name="arg3">Third parameter for the Action delegate.</param>
        /// <returns>The return value is determined by the return value of the action itself.</returns>
        public object JavaScriptInvokeAction3(string action, object canExecuteParam,
                                              object alternativeActionParam, object arg1, object arg2, object arg3)
        {
            return InvokeAction(action, canExecuteParam, alternativeActionParam, arg1);
        }

        private class ActionHolder
        {
            public Delegate Action { get; set; }
            public Func<object, bool> CanExecute { get; set; }
            public Action<object> AlternativeAction { get; set; }
        }
    }
}